<?php


namespace App\Services\Customer;

use App\Common\BnFormatConfig;
use App\Common\ResponseCode;
use App\Common\Tools;
use App\Exceptions\BusinessException;
use App\Models\Customer\Customer;
use App\Models\Customer\Grade;
use App\Models\Customer\Group;
use App\Params\Backend\CustomerListParams;
use App\Repository\AddressRepository;
use App\Repository\CouponRepository;
use App\Repository\CustomerRepository;
use App\Repository\GradeRepository;
use App\Services\BaseService;
use App\Services\Promotion\CouponService;
use Carbon\Carbon;

/**
 * 会员相关业务类
 *
 * @author WangBin
 * @date 2021-09-20
 * @package App\Http\Services
 */
class CustomerService extends BaseService
{
    /**
     * 根据用户名查询用户
     *
     * @param $username
     * @return
     */
    public function getByUsername(string $username)
    {
        return CustomerRepository::getInstance()->getByUsername($username);
    }

    /**
     * 根据手机号查询用户
     *
     * @param string $mobile
     * @return
     */
    public function getByMobile(string $mobile)
    {
        return CustomerRepository::getInstance()->getByMobile($mobile);
    }

    /**
     * 根据ID查询客户
     *
     * @param int $customerId
     * @return
     */
    public function getById(int $customerId)
    {
        return CustomerRepository::getInstance()->getById($customerId);
    }

    /**
     * 更新客户信息
     *
     * @param int $customerId
     * @param array $data
     * @return bool
     * @throws BusinessException
     */
    public function update(int $customerId, array $data)
    {
        $customer = CustomerRepository::getInstance()->getById($customerId);
        if(is_null($customer)) {
            throw new BusinessException(ResponseCode::CUSTOMER_NOT_FOUND);
        }
        return CustomerRepository::getInstance()->saveData($data);
    }

    /**
     * 获取客户列表
     *
     * @param CustomerListParams $filter
     * @return
     */
    public function getList(CustomerListParams $filter)
    {
        $customers = CustomerRepository::getInstance()->getList($filter);
        return $this->toList($customers, $filter->getPage(), $filter->getPageSize());
    }

    /**
     * 会员自动注册
     *
     * @param string $mobile
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @throws BusinessException
     */
    public function autoRegister(string $mobile)
    {
        $data = [
            'customer_bn' => Tools::buildBn(BnFormatConfig::CUSTOMER_BN_KEY),
            'nickname' => $mobile,
            'mobile' => $mobile,
            'username' => $mobile,
            'grade_id' => 1,
            'group_id' => 4
        ];
        $customerId = CustomerRepository::getInstance()->saveData($data);
        return CustomerRepository::getInstance()->getById($customerId);
    }

    /**
     * 更新客户登录信息
     *
     * @param int $customerId
     * @return int|mixed
     */
    public function updateLoginInfo(int $customerId)
    {
        $data = [
            'customer_id' => $customerId,
            'last_login_at' => Carbon::now()
        ];
        return CustomerRepository::getInstance()->saveData($data);
    }

    /**
     * 统计客户财产信息，包含余额、优惠券、积分
     *
     * @param int $customerId
     * @return array
     */
    public function getAssets(int $customerId)
    {
        $customer = CustomerRepository::getInstance()->getById($customerId);
        // 获取客户可用的优惠券数量
        $couponTotal = CouponService::getInstance()->getCustomerCouponNum($customerId);
        return [
            'balance' => $customer->balance,
            'coupon' => $couponTotal,
            'points' => 0
        ];
    }

    /**
     * 获取会员详情信息
     *
     * @param int $customerId
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getCustomerInfo(int $customerId)
    {
        $customer = CustomerRepository::getInstance()->getById($customerId);
        $customer->grade_name = GradeRepository::getInstance()->getById($customer->grade_id)->grade_name;
        return $customer;
    }

    /**
     * 统计会员数量
     *
     * @param array $filter
     * @return int
     */
    public function getCount(array $filter = [])
    {
        return CustomerRepository::getInstance()->getCountNum($filter);
    }
}
